package com.hsd.netty.server.strategy;

import com.hsd.netty.server.common.NettyHeader;
import com.hsd.netty.server.common.NettyMessage;
import com.hsd.netty.server.enums.MessageCode;
import com.hsd.netty.server.enums.MessageType;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * @author s.he
 * on 2020/6/1
 */
public class LoginMessageHandler extends MessageTypeHandler{
    private static final Logger logger = LoggerFactory.getLogger(LoginMessageHandler.class);


    @Override
    public NettyMessage handler(ChannelHandlerContext channelHandlerContext, NettyMessage nettyMessage) {

        InetSocketAddress socketAddress = (InetSocketAddress)channelHandlerContext.channel().remoteAddress();
        String hostAddress = socketAddress.getAddress().getHostAddress();
        logger.info("【登录操作】客户端 " + hostAddress + "正在登录");
        NettyMessage result = new NettyMessage();
        NettyHeader header = new NettyHeader();
        header.setType(MessageType.LOGIN.getCode());
        if(blackIPAddress.contains(hostAddress)) {
            long timeMillis = System.currentTimeMillis();
            loginInfoMap.put(hostAddress, String.valueOf(timeMillis));
            header.setSessionId(timeMillis);
            header.setStatusCode(MessageCode.SUCCESS.getCode());
            logger.info("【登录操作】客户端 " + hostAddress + "登录成功");
        } else {
            header.setStatusCode(MessageCode.ERROR.getCode());
            logger.info("客户端 " + hostAddress + "登录失败");
        }
        result.setNettyHeader(header);
        return result;
    }
}
